package com.hnuit.service;

import com.hnuit.Utils.MybatisUtils;
import com.hnuit.dao.UserDao;
import com.hnuit.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDemo {
    @Test//查寻所有操作
    public void findAll() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        UserDao dao=session.getMapper(UserDao.class);
        List<User> all=dao.findAll();
        System.out.println(all);
        session.close();
    }
    @Test//单个寻找操作
    public void findById() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        UserDao dao=session.getMapper(UserDao.class);
        User user=dao.findById(1);
        System.out.println(user);
        session.close();
    }
    @Test//删除操作
    public void deleteById() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        UserDao dao=session.getMapper(UserDao.class);
        User user=dao.deleteById(3);
        System.out.println("已删除");
        session.commit();
        session.close();
    }
    @Test//增加操作
    public void addUser() throws IOException {
        SqlSession session= MybatisUtils.getSession();
        UserDao dao=session.getMapper(UserDao.class);
        User user=new User();
        user.setId(3);
        user.setUsername("韩红");
        user.setPassword("23456");
        user.setEmail("313q@qq.com");
        dao.addUser(user);
        session.commit();
        session.close();
    }
    @Test//更新操作
    public void updateUser() throws IOException{
        SqlSession session=MybatisUtils.getSession();
        UserDao dao=session.getMapper(UserDao.class);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",3);
        map.put("username","哈哈哈");
        map.put("password","99999");
        map.put("email","hello@qq.com");
        dao.updateUser(map);
        session.commit();
        session.close();
    }
}
